package org.jash.shopdemo2212;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity {
    private EditText username, password, confirm;
    private Button getVerify;
    private final Handler handler = new Handler(Looper.getMainLooper());
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        confirm = findViewById(R.id.confirm);
        getVerify = findViewById(R.id.get_verify);
        getVerify.setOnClickListener(v -> {
            getVerify.setEnabled(false);
            for (int i = 0; i < 60; i++) {
                String s = (60 - i) + "秒后重新获取";
                handler.postDelayed(() -> getVerify.setText(s), i * 1000);
            }
            handler.postDelayed(() -> {
                getVerify.setText("获取验证码");
                getVerify.setEnabled(true);
            }, 60000);
        });
        findViewById(R.id.register).setOnClickListener(v -> {
            String un = username.getText().toString();
            String pw = password.getText().toString();
            String con = confirm.getText().toString();
            if (un.isEmpty() || pw.isEmpty()) {
                Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!pw.equals(con)) {
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            HttpUtils.doPost("http://10.161.52.182:8080/user/register", new HashMap<String, Object>() {{
                        // 写入参数
                        put("username", un);
                        put("password", pw);
                    }}, UserEntity.class,
                    r -> {
                        // 正确的处理
                        if (r.getCode() == 200) {
                            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                            setResult(RESULT_OK, new Intent().putExtra("username", un).putExtra("password", pw));
                            finish();
                        } else {
                            Toast.makeText(this, r.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }, t -> {
                        // 错误的处理
                        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    });

        });
        findViewById(R.id.goto_login).setOnClickListener(v -> {
            finish();
        });
    }
}